package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import CtrLayer.SaleCtr;
import ModelLayer.Employee;
import ModelLayer.PartSale;
import ModelLayer.Sale;

/**
 * DraughtBeerGUI
 * this class creates the draughtBeerGUIPanel which contains a selection of draught beers.
 *  @author Lindgren
 */
public class DraughtBeerGUI {

	private JPanel draughtBeerGUIPanel;
	private Employee employee;
	private String priceType = "defaultPrice";
	private JComboBox comboBox;
	private JButton btnStaff;
	private JTextArea salesList;
	private JTextField total;
	private JButton btnHalfPint;
	private JButton btnVip;
	private JButton btnVip_1;


	/**
	 * Create a draughtBeerGUI.
	 * @param employee 
	 */
	public DraughtBeerGUI(Employee employee) {
		this.employee = employee; 
		initialize();
		System.out.println("DraughtBeerGUI initialized!");
	}

	/**
	 * Initialize the panel.
	 */
	public void initialize() {

		System.out.println("DraughtBeerGUI initializing");

		draughtBeerGUIPanel = new JPanel();
		draughtBeerGUIPanel.setToolTipText("draughtBeerGUIPanel");
		draughtBeerGUIPanel.setForeground(Color.BLACK);


		draughtBeerGUIPanel.setName(employee.getLogin());

		MainFrame.getDraughtBeerGUIPanels().put(employee.getLogin(), draughtBeerGUIPanel);
		System.out.println("DraughtBeerGUI draughtBeerGUIPanel mapped: "+MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()));


		draughtBeerGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		Box horizontalBox = Box.createHorizontalBox();
		draughtBeerGUIPanel.add(horizontalBox);

		JPanel panel = new JPanel();
		horizontalBox.add(panel);
		panel.setToolTipText("beerPanel");
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_8 = new JPanel();
		panel.add(panel_8);
		panel_8.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnCarlsberg = new JButton("Carlsberg");
		btnCarlsberg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProduct(10);
			}
		});
		panel_8.add(btnCarlsberg);

		JButton btnCarlssSpecial = new JButton("Carls's Special");
		btnCarlssSpecial.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProduct(11);
			}
		});
		panel_8.add(btnCarlssSpecial);

		JButton btnGuinness = new JButton("Guinness");
		btnGuinness.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(2);
			}
		});
		panel_8.add(btnGuinness);

		JPanel panel_7 = new JPanel();
		panel.add(panel_7);
		panel_7.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnKilkenny = new JButton("Kilkenny");
		btnKilkenny.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(3);
			}
		});
		panel_7.add(btnKilkenny);

		JButton btnHoeggaarden = new JButton("Hoeggaarden");
		btnHoeggaarden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(8);
			}
		});
		panel_7.add(btnHoeggaarden);

		JButton btnNewcastleBrownAle = new JButton("Newcastle Brown Ale");
		btnNewcastleBrownAle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(9);
			}
		});
		panel_7.add(btnNewcastleBrownAle);

		JPanel panel_6 = new JPanel();
		panel.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnKronenburg = new JButton("Kronenburg 1664");
		btnKronenburg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(7);
			}
		});
		panel_6.add(btnKronenburg);

		JButton btnPaulanerWeiss = new JButton("Paulaner Weiss");
		btnPaulanerWeiss.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(15);
			}
		});
		panel_6.add(btnPaulanerWeiss);

		JButton btnIrishHouseAle = new JButton("Irish House Ale");
		btnIrishHouseAle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(1);
			}
		});
		panel_6.add(btnIrishHouseAle);

		JPanel panel_5 = new JPanel();
		panel.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnMurphysRed = new JButton("Murphy's Red");
		btnMurphysRed.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(6);
			}
		});
		panel_5.add(btnMurphysRed);

		JButton btnMurphysStout = new JButton("Murphy's Stout");
		btnMurphysStout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(5);
			}
		});
		panel_5.add(btnMurphysStout);

		JButton btnIrishHousePale = new JButton("Irish House Pale Ale");
		btnIrishHousePale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(4);
			}
		});
		panel_5.add(btnIrishHousePale);

		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnCrowmorePearCider = new JButton("Crowmore Pear Cider");
		btnCrowmorePearCider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(13);
			}
		});
		panel_4.add(btnCrowmorePearCider);

		JButton btnCrowmoreAppleCider = new JButton("Crowmore Apple Cider");
		btnCrowmoreAppleCider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(14);
			}
		});
		panel_4.add(btnCrowmoreAppleCider);

		JButton btnLeffeBlonde = new JButton("Leffe Blonde");
		btnLeffeBlonde.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(12);
			}
		});
		panel_4.add(btnLeffeBlonde);

		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnShandy = new JButton("Shandy");
		btnShandy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(17);
			}
		});
		panel_3.add(btnShandy);

		JButton btnSnakeBite = new JButton("Snake Bite");
		btnSnakeBite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(16);
			}
		});
		panel_3.add(btnSnakeBite);

		JButton btnIrishBlend = new JButton("Irish Blend");
		btnIrishBlend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(18);
			}
		});
		panel_3.add(btnIrishBlend);

		JPanel panel_1 = new JPanel();
		horizontalBox.add(panel_1);
		panel_1.setLayout(null);

		JPanel VIPPanel = new JPanel();
		VIPPanel.setBounds(10, 5, 385, 80);
		panel_1.add(VIPPanel);
		VIPPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_9 = new JPanel();
		VIPPanel.add(panel_9);
		panel_9.setLayout(new GridLayout(0, 3, 0, 0));

		btnHalfPint = new JButton("Half Pint");
		btnHalfPint.setBackground(UIManager.getColor("Button.background"));
		btnHalfPint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// change the pricetype

				try {
					priceType = "halfPint";	
					btnHalfPint.setBackground(Color.GREEN);

					btnStaff.setBackground(UIManager.getColor("Button.background"));

					btnVip.setBackground(UIManager.getColor("Button.background"));
					btnVip_1.setBackground(UIManager.getColor("Button.background"));
				} catch (Exception e) {
					System.err.println("changing pricetype failed");
					e.printStackTrace();
				}
			}
		});
		panel_9.add(btnHalfPint);

		Component glue_1 = Box.createGlue();
		panel_9.add(glue_1);

		Component glue = Box.createGlue();
		panel_9.add(glue);

		JPanel panel_10 = new JPanel();
		VIPPanel.add(panel_10);
		panel_10.setLayout(new GridLayout(0, 3, 0, 0));

		btnStaff = new JButton("Staff");
		btnStaff.setBackground(UIManager.getColor("Button.background"));
		btnStaff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// change the pricetype
				try {
					priceType = "staff";	
					btnStaff.setBackground(Color.GREEN);
					btnHalfPint.setBackground(UIManager.getColor("Button.background"));
					btnVip.setBackground(UIManager.getColor("Button.background"));
					btnVip_1.setBackground(UIManager.getColor("Button.background"));
				} catch (Exception e1) {
					System.err.println("changing pricetype failed");
					e1.printStackTrace();
				}
			}
		});
		panel_10.add(btnStaff);


		btnVip = new JButton("VIP #1");
		btnVip.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// change the pricetype
				try {
					priceType = "VIP1";	
					btnVip.setBackground(Color.GREEN);
					btnStaff.setBackground(UIManager.getColor("Button.background"));
					btnHalfPint.setBackground(UIManager.getColor("Button.background"));
					btnVip_1.setBackground(UIManager.getColor("Button.background"));
				} catch (Exception e1) {
					System.err.println("changing pricetype failed");
					e1.printStackTrace();
				}
			}
		});
		panel_10.add(btnVip);

		btnVip_1 = new JButton("VIP #2");
		btnVip_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// change the pricetype
				try {
					priceType = "VIP2";	
					btnVip_1.setBackground(Color.GREEN);
					btnStaff.setBackground(UIManager.getColor("Button.background"));
					btnHalfPint.setBackground(UIManager.getColor("Button.background"));
					btnVip.setBackground(UIManager.getColor("Button.background"));
				} catch (Exception e1) {
					System.err.println("change pricetype failed");
					e1.printStackTrace();
				}
			}
		});
		panel_10.add(btnVip_1);

		JPanel panel_11 = new JPanel();
		VIPPanel.add(panel_11);
		panel_11.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblAmount = new JLabel("Amount");
		lblAmount.setHorizontalAlignment(SwingConstants.CENTER);
		panel_11.add(lblAmount);

		Component glue_2 = Box.createGlue();
		panel_11.add(glue_2);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8"}));
		panel_11.add(comboBox);

		JPanel SalesListPanel = new JPanel();
		SalesListPanel.setBounds(10, 96, 385, 232);
		panel_1.add(SalesListPanel);
		SalesListPanel.setBorder(new TitledBorder(null, "Sales List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		SalesListPanel.setLayout(new GridLayout(0, 1, 0, 0));

		salesList = new JTextArea();
		salesList.setEditable(false);
		SalesListPanel.add(salesList);

		JPanel panel_12 = new JPanel();
		draughtBeerGUIPanel.add(panel_12);
		panel_12.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_13 = new JPanel();
		panel_12.add(panel_13);
		panel_13.setLayout(null);

		total = new JTextField();
		total.setBackground(Color.WHITE);
		total.setEditable(false);
		total.setBounds(1040, 11, 137, 20);
		panel_13.add(total);
		total.setColumns(10);

		JLabel lblTotal = new JLabel("Total:");
		lblTotal.setBounds(984, 14, 46, 14);
		panel_13.add(lblTotal);

		JLabel lblKr = new JLabel("Kr.");
		lblKr.setBounds(1188, 14, 24, 14);
		panel_13.add(lblKr);

		JPanel panel_2 = new JPanel();
		panel_12.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 4, 0, 0));

		JButton btnBack_1 = new JButton("Back");
		panel_2.add(btnBack_1);
		btnBack_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});
		btnBack_1.setBackground(Color.YELLOW);

		JButton btnMinus_1 = new JButton("Minus");
		btnMinus_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				minus();
				updateSalesList();
			}
		});
		panel_2.add(btnMinus_1);
		btnMinus_1.setForeground(Color.WHITE);
		btnMinus_1.setBackground(Color.RED);

		Component glue_3 = Box.createGlue();
		panel_2.add(glue_3);

		JButton btnGoToPayment_1 = new JButton("Go To Payment");
		panel_2.add(btnGoToPayment_1);
		btnGoToPayment_1.setForeground(Color.WHITE);
		btnGoToPayment_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				endSale();
				openPaymentGUI();
			}
		});
		btnGoToPayment_1.setBackground(Color.GREEN);

		JPanel panel_14 = new JPanel();
		panel_12.add(panel_14);
	}

	/**
	 * Removes the current panel (GUI) and sets the paymentPanel instead.
	 */
	private void openPaymentGUI()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(draughtBeerGUIPanel);//MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
			MainFrame.getPaymentGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getPaymentGUIs().get(employee.getLogin()).updateSalesList();
			priceType = "defaultPrice";
		} catch (Exception e) {
			System.err.println("opening paymentGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Calculates the total price on the sale using the salecontroller.
	 */
	private void endSale()
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			SaleCtr.getInstance().endSale(sale);
		} catch (Exception e) {
			System.err.println("Calculating total price failed");
			e.printStackTrace();
		}
	}

	/**
	 * @return the priceType
	 */
	public String getPriceType() {
		return priceType;
	}

	/**
	 * @param priceType the priceType to set
	 */
	public void setPriceType(String priceType) {
		this.priceType = priceType;
	}

	/**
	 * Updates the sales list JTextArea.
	 */
	public void updateSalesList()
	{
		try {
			salesList.setText("");
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			ArrayList<PartSale> partSales = sale.getPartsales();
			for(PartSale partSale : partSales)
			{
				salesList.insert((partSale.getAmount() + " x " + partSale.getProduct().getName() + " : " + partSale.getPrice() + " kr. \n"), 0);
			}
			SaleCtr saleCtr = SaleCtr.getInstance();
			saleCtr.endSale(sale);
			total.setText(String.valueOf(sale.getTotal()));
		} catch (Exception e) {
			System.err.println("updating salesList textArea failed");
			e.printStackTrace();
		}
	}

	/**
	 * Adds a product to a partSale and add that partSale to the current sale using the sale controller.
	 * Also resets all special price buttons (staff, halfPint, VIP1 and VIP2) and resets priceType to default.
	 */
	private void addProduct(int productID)
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			SaleCtr.getInstance().addProduct(productID, comboBox.getSelectedIndex()+1, priceType, sale);

			btnStaff.setBackground(UIManager.getColor("Button.background"));
			btnHalfPint.setBackground(UIManager.getColor("Button.background"));
			btnVip.setBackground(UIManager.getColor("Button.background"));
			btnVip_1.setBackground(UIManager.getColor("Button.background"));
			comboBox.setSelectedIndex(0);

			updateSalesList();
			priceType = "defaultPrice";
		} catch (Exception e) {
			System.err.println("adding product to partsale and partsale to sale failed");
			e.printStackTrace();
		}
	}

	/**
	 * Removes the last partSale from the sale 
	 */
	private void minus()
	{
		try {
			SaleCtr.getInstance().removePartSale( MainFrame.getSaleGUIs().get(employee.getLogin()).getSale() );
		} catch (Exception e) {
			System.err.println("removing partsale failed");
			e.printStackTrace();
		}
	}

	/**
	 * returns the user to the saleGUI 
	 */
	private void back()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).repaint();
			MainFrame.getSaleGUIs().get(employee.getLogin()).updateSalesList();
			priceType = "defaultPrice";
		} catch (Exception e) {
			System.err.println("returning to saleGUI failed");
			e.printStackTrace();
		}
	}
}
